package com.asura.app.wechat.ui.moment;

import android.util.Pair;

import com.asura.app.wechat.base.mvp.BasePresenter;
import com.asura.app.wechat.common.WechatModuleProvider;
import com.asura.app.wechat.repository.datasource.MomentDataSource;
import com.asura.app.wechat.repository.datasource.UserInfoDataSource;
import com.asura.app.wechat.repository.model.Tweet;
import com.asura.app.wechat.repository.model.UserInfo;

import java.util.List;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.BiFunction;
import io.reactivex.observers.DisposableSingleObserver;

/**
 * Created by Asura on 2017/12/29.
 */

public class MomentPresenter extends BasePresenter<MomentContract.View> implements MomentContract.Presenter {

    private static final int PAGE_SIZE = 5;
    private final MomentDataSource momentDataSource;
    private final UserInfoDataSource userInfoDataSource;

    public MomentPresenter() {
        momentDataSource = WechatModuleProvider.getInstance().getMomentDataSource();
        userInfoDataSource = WechatModuleProvider.getInstance().getUserInfoDataSource();
    }

    @Override
    public void initLoadTweets() {
        cancelAllSubscriptions();
        getMvpView().showInitLoadLoading();
        DisposableSingleObserver<Pair<UserInfo, List<Tweet>>> observer = Single.zip(
                userInfoDataSource.getUserInfo(),
                momentDataSource.getTweets(null, PAGE_SIZE),
                new BiFunction<UserInfo, List<Tweet>, Pair<UserInfo, List<Tweet>>>() {
                    @Override
                    public Pair<UserInfo, List<Tweet>> apply(UserInfo userInfo, List<Tweet> tweets) throws Exception {
                        return new Pair<>(userInfo, tweets);
                    }
                }
        )
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSingleObserver<Pair<UserInfo, List<Tweet>>>() {
                    @Override
                    public void onSuccess(Pair<UserInfo, List<Tweet>> userInfoListPair) {
                        getMvpView().showInitLoadResult(userInfoListPair.first, userInfoListPair.second);
                    }

                    @Override
                    public void onError(Throwable e) {
                        // TODO: 2017/12/29 error handler
                        getMvpView().showInitLoadError(e.getMessage());
                    }
                });
        addSubscription(observer);
    }

    @Override
    public void loadMoreTweets(String id) {
        cancelAllSubscriptions();
        getMvpView().showLoadMoreLoading();
        DisposableSingleObserver<List<Tweet>> observer = momentDataSource.getTweets(id, PAGE_SIZE)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSingleObserver<List<Tweet>>() {
                    @Override
                    public void onSuccess(List<Tweet> tweets) {
                        getMvpView().showLoadMoreResult(tweets);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getMvpView().showLoadMoreError(e.getMessage());
                    }
                });
        addSubscription(observer);
    }

    @Override
    public void refreshTweets() {
        cancelAllSubscriptions();
        getMvpView().showRefreshLoading();
        DisposableSingleObserver<Pair<UserInfo, List<Tweet>>> observer = Single.zip(
                userInfoDataSource.getUserInfo(),
                momentDataSource.getTweets(null, PAGE_SIZE),
                new BiFunction<UserInfo, List<Tweet>, Pair<UserInfo, List<Tweet>>>() {
                    @Override
                    public Pair<UserInfo, List<Tweet>> apply(UserInfo userInfo, List<Tweet> tweets) throws Exception {
                        return new Pair<>(userInfo, tweets);
                    }
                }
        )
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSingleObserver<Pair<UserInfo, List<Tweet>>>() {
                    @Override
                    public void onSuccess(Pair<UserInfo, List<Tweet>> userInfoListPair) {
                        getMvpView().showRefreshResult(userInfoListPair.first, userInfoListPair.second);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getMvpView().showRefreshError(e.getMessage());
                    }
                });
        addSubscription(observer);
    }

    @Override
    public void cancelRefreshTweets() {
        cancelAllSubscriptions();
    }
}
